﻿using System;
using System.Xml;

namespace TaskWin {

	public struct Settings {
		public String version;

		public String taskFolder;
		public String lastVisit;
		public String autoUpdate;
		public String showCalendar;

		public String winY;
		public String winX;
		public String winW;
		public String winH;
	}

	public class SettingsMgr {
		XmlDocument xmlDoc = new XmlDocument();
		XmlNode temp;
		Settings conf;
		String settingsFileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\TaskWin.xml";

		public Settings read() {
			try {
				if (System.IO.File.Exists(settingsFileName)) xmlDoc.Load(settingsFileName);
				else throw new XmlException("Settings file not found");
			}
			catch (XmlException e) { }
			finally { conf = setDefaultSettings(); }

			if (xmlDoc != null) {

				XmlNode settings = xmlDoc.SelectSingleNode("/settings");
				if (settings != null) {
					temp = settings.SelectSingleNode("taskFolder");
					if (temp != null) conf.taskFolder = temp.InnerText;

					temp = settings.SelectSingleNode("autoUpdate");
					if (temp != null) conf.autoUpdate = temp.InnerText;

					temp = settings.SelectSingleNode("showCalendar");
					if (temp != null) conf.showCalendar = temp.InnerText;

					temp = settings.SelectSingleNode("lastVisit");
					if (temp != null) conf.lastVisit = temp.InnerText;
				}
				XmlNode win = xmlDoc.SelectSingleNode("/settings/window");
				if (win != null) {
					temp = win.SelectSingleNode("x");
					if (temp != null) conf.winX = temp.InnerText;

					temp = win.SelectSingleNode("y");
					if (temp != null) conf.winY = temp.InnerText;

					temp = win.SelectSingleNode("w");
					if (temp != null) conf.winW = temp.InnerText;

					temp = win.SelectSingleNode("h");
					if (temp != null) conf.winH = temp.InnerText;
				}

			}
			return conf;
		}

		private Settings setDefaultSettings() {
			Settings conf;
			conf.taskFolder="";
			conf.lastVisit = "";
			conf.autoUpdate = "false";
			conf.showCalendar = "false";
			conf.version = this.getVersion();
			conf.winX = "120";
			conf.winY = "120";
			conf.winH = "500";
			conf.winW = "500";
			return conf;
		}

		public void save(Settings conf) {
			XmlTextWriter writer = new XmlTextWriter(settingsFileName, System.Text.Encoding.UTF8);
			writer.WriteStartDocument();
			writer.WriteRaw("\n<settings>\n");
			writer.WriteRaw("	<taskFolder>" + conf.taskFolder + "</taskFolder>\n");
			writer.WriteRaw("	<autoUpdate>" + conf.autoUpdate + "</autoUpdate>\n");
			writer.WriteRaw("	<showCalendar>" + conf.showCalendar + "</showCalendar>\n");
			writer.WriteRaw("	<lastVisit>" + DateTime.Now.Date.ToString("yyyyMMdd") + "</lastVisit>\n");

			writer.WriteRaw("	<window>\n");
			writer.WriteRaw("		<x>" + conf.winX + "</x>\n");
			writer.WriteRaw("		<y>" + conf.winY + "</y>\n");
			writer.WriteRaw("		<w>" + conf.winW + "</w>\n");
			writer.WriteRaw("		<h>" + conf.winH + "</h>\n");
			writer.WriteRaw("	</window>\n");
			writer.WriteRaw("</settings>");
			writer.Close();
		}


		public String getVersion() {
			Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
			return version.ToString();
		}

	}

}
